﻿@using System.Web.Script.Serialization
@{
    ViewBag.Title = "EditAttributeValues";
    Layout = "~/views/shared/_AdminLayout.cshtml";
}

<h4>EditAttributeValues</h4>
@{
    
    var model = ViewBag.Model;
    var jsonStr = new HtmlString(new JavaScriptSerializer().Serialize(model));
}
<label>attribute name:</label>
<span data-bind="text:Name"></span>

<label>Value List</label>
<table class="table">
    <tr>
        <th>@T("admin.productvariantvalues.name.title")</th>
        <th>@T("admin.productvariantvalues.displayorder.title")</th>
        <th></th>

    </tr>
    <!-- ko foreach: Values -->
    <tr>
        <td>
           <span data-bind="text:Name"></span>
        </td>
        <td>
            <span data-bind="text:DisplayOrder"></span>
        </td>
        <td>
             <a data-bind="click:$root.DeleteProductVariantAttributeValue">@T("admin.productvariantattributevalue.delete.title")</a>
        </td>
    </tr>
<!-- /ko -->
</table>

<label>Value</label>
<input type="text" data-bind="value:Value"/>

    <p data-bind="validationMessage: Value" class="text-error"></p>


<label>Display Order</label>
<input type="text" data-bind="value:DisplayOrder"/>

    <p data-bind="validationMessage: DisplayOrder" class="text-error"></p>

<div>
    <input type="button" value="Save" data-bind="click:SaveProductVariantAttributeValue"/>
    <a  data-bind="attr:{href:'/admin/productvariant/edit?productvariantid='+PvId()+'&tab=pva'}" class="btn btn-link">@T("Admin.ProductVariantAttributeValues.Save.Back")</a>
</div>
@section scripts
{
    
    <script>
        var init = @jsonStr;

        function ProductVariantAttributeValue(name, displayOrder) {
            var self = this;
            self.Name = ko.observable(name);
            self.DisplayOrder = ko.observable(displayOrder);
        }

        function ProductVariantAttributeValueModel() {

            var self = this;
            self.PvId = ko.observable(init.PvId);
            self.ProductVariantAttributeId = ko.observable(init.ProductVariantAttributeId);
            self.Name = ko.observable(init.Name);

            self.Value = ko.observable().extend({
                required: { message: '@T("Admin.ProductVariantAttributeValue.NeedValue")' },
            });

            self.DisplayOrder = ko.observable().extend({
                required: { message: '@T("Admin.ProductVariantAttributeValue.NeedDisplayOrder")' },
                digit: { message: '@T("Admin.ProductVariantAttributeValue.DisplayOrderFormatError")' }
            });

            
            var tempValues = $.isEmptyObject(init.Values) ? [] : $.map(init.Values, function(a) {
                return new ProductVariantAttributeValue(a.Name, a.DisplayOrder);
            });

            self.Values = ko.observableArray(tempValues);

            self.DeleteProductVariantAttributeValue = function(p) {
                if (confirm('@T("admin.productvariantattributevalue.delete.confirm")')) {
                    var data = {
                        ProductvariantattributeValueId: p.Id()
                    };
                    

                    $.ajax({
                        url: "/api/ProductVariantAttributeApi/DeleteProductVariantAttributeValue",
                        type: 'post',
                        data: ko.toJSON(data),
                        contentType: 'application/json',
                        success: function(result) {

                            if (result.Status == "0") {
                                self.Values.remove(p);
                                toastr.success(result.Message);
                                

                            } else {
                                toastr.error(result.Message);
                            }
                        }
                    });
                }
            };
            self.SaveProductVariantAttributeValue = function() {
                if (this.isValid()) {
                    $.ajax({
                        url: "/api/ProductVariantAttributeApi/InsertProductVariantAttributeValue",
                        type: 'post',
                        data: ko.toJSON(self),
                        contentType: 'application/json',
                        success: function(result) {

                            if (result.Status == "0") {

                                toastr.success(result.Message);
                                self.Values.push(new  ProductVariantAttributeValue(self.Value(), self.DisplayOrder()));
                                self.Value('');
                                self.DisplayOrder('');

                            } else {
                                toastr.error(result.Message);
                            }
                        }
                    });
                } else {
                    self.errors.showAllMessages();
                }
            };
        }

        var vm = new ProductVariantAttributeValueModel();
        vm.errors = ko.validation.group(vm);
        ko.applyBindings(vm);
    </script>
}
